import requests
from lxml import etree


from fake_useragent import UserAgent
from lxml.cssselect import  CSSSelector
from lxml import html
# xml = """<book id="1">
#   <title>Harry Potter</title>
#
#   1111111111111
#   <author>J.K. Rowling
#   <h1></h1>
#   </author>
# </book>"""
#
# datas = etree.XML(xml)
#
# print(dir(etree))
# print(dir(datas))
# print(datas.attrib)
# print(datas.tag)
# print(datas.text)
# print(datas.find("title"))
# print(datas.find("author/h1"))
#
#
# html_data = """
# <!DOCTYPE html>
# <html>
#   <body>
#     <p>Hello
#     <br>
#     <img src=photo.jpg>
#   </body>
# </html>
#
#
# """

# datas = html.fromstring(html_data)
# datas = etree.HTML(html_data)
# print(datas.tag)
# print(datas.text)
# print(datas.find("body/p"))
# print(datas.find(".//p"))




#选择器

#
# html_data = """
# <!DOCTYPE html>
# <html>
#   <head>
#     <title>测试页面</title>
#   </head>
#   <body>
#     <div class="container">
#       <p id="intro">欢迎来到我的网站</p>
#       <p class="content">这是第一段内容</p>
#       <p class="content highlight">这是第二段重要内容</p>
#       <ul>
#         <li>列表项1</li>
#         <li>列表项2</li>
#         <li class="active">列表项3</li>
#       </ul>
#         <ol>
#             <li><a href="https://example.com" class="link external">外部链接</a></li>
#             <li><a href="https://example.com" class="link external">外部链接</a></li>
#             <li class="active"><a href="https://example.com" class="link external">外部链接</a></li>
#         </ol>
#       <a href="https://example.com" class="link external">外部链接</a>
#       <img src="image.jpg" alt="示例图片" class="thumbnail">
#     </div>
#   </body>
# </html>
# """


# tree = etree.HTML(html_data)
#
# datas = tree.cssselect('ol li')
#
# print(type(datas))
#
#
# for data in datas:
#     print(data.text,data.tag,data.attrib)
#     a_tag = data.cssselect('a')
#     for child in a_tag:
#         print(child.text,child.tag,child.attrib)





#Xpath


#
# html_data = """
# <html>
#
#   <body>
#   <h3>智能手机</h3>
#     <div class="products">
#       <div class="product" data-category="electronics" data-price="2999">
#         <h3>智能手机</h3>
#         <span class="price">¥2999</span>
#         <button class="add-cart">加入购物车</button>
#       </div>
#       <nav>
#         <ul class="nav-menu">
#           <li><a href="/home" class="nav-link active">首页</a></li>
#           <li><a href="/products" class="nav-link">商品</a></li>
#           <li><a href="/about" class="nav-link">关于我们</a></li>
#         </ul>
#          <ul class="nav-menu1">
#           <li><a href="/home" class="nav-link active">首页</a></li>
#           <li><a href="/products" class="nav-link">商品</a></li>
#           <li><a href="/about" class="nav-link">关于我们</a></li>
#         </ul>
#       </nav>
#       <div class="product featured" data-category="clothing" data-price="299">
#         <h3>T恤</h3>
#         <span class="price">¥299</span>
#         <button class="add-cart">加入购物车</button>
#       </div>
#     </div>
#   </body>
# </html>
# """
#
#
# tree = etree.HTML(html_data)
#
# ul = tree.xpath('//ul[@class="nav-menu"]/li[position()>1]/a')
# for u in ul:
#     print(u.text,u.tag,u.attrib)





#任意路劲下的h3
# h3 = tree.xpath('//h3')

# h33 = tree.xpath('//body')
# for h in h33:
#     print(h.tag)

# ul = tree.xpath('//ul[@class="nav-menu"]')
# for u in ul:
#     u = u.xpath('.//li')
#     for u in u:
#         print(u.text,u.tag,u.attrib)

    # print(u.tag,u.attrib)
    # li = u.xpath('./li/a')
    # for li in li :
    #     print(li.text,li.tag,li.attrib)

# print(h3)


ua = UserAgent()

uagent = ua.random

url = "http://www.vcg.com/"
print(uagent)
header = {
    "User-Agent":uagent
}

response = requests.get(url,headers=header)
print(response.status_code)
text = response.text
tree = etree.HTML(text)
datas = tree.xpath("//figure[@class='_3bNiX']//picture[@class='_2t2n7']/@style")

print(datas)

for data in datas:
    print(data)




